package application.controller;

import application.adapter.ListAdapter;
import application.anno.Column;
import application.anno.Layout;
import application.anno.StageTitle;
import application.windows.UserInterface;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

@Layout("ListDemo.fxml")
@StageTitle("列表视图展示")
public class ListDemo extends UserInterface {

    @Data
    @Accessors(chain = true)
    public static class Entry {

        @Column(filedName = "编号", widthProportion = 0.2)
        private int id;

        @Column(filedName = "键", widthProportion = 0.2)
        private String key;

        @Column(filedName = "值", widthProportion = 0.2)
        private String value;

        @Column(filedName = "附加", widthProportion = 0.2)
        private String extra;

        @Column(filedName = "说明", widthProportion = 0.2)
        private String remark;
    }

    @FXML
    private Button backup;

    @FXML
    private ListView<Pane> showList;

    @Override
    @FXML
    protected void initialize() throws IllegalAccessException {
        super.initialize();
        List<Entry> tests = new ArrayList<>();
        Entry entry = new Entry()
                .setId(1)
                .setKey("key1")
                .setValue("value1")
                .setExtra("extra1")
                .setRemark("remark1");
        tests.add(entry);
        entry = new Entry()
                .setId(2)
                .setKey("key2")
                .setValue("value2")
                .setExtra("extra2")
                .setRemark("remark2");
        tests.add(entry);
        entry = new Entry()
                .setId(3)
                .setKey("key3")
                .setValue("value3")
                .setExtra("extra3")
                .setRemark("remark3");
        tests.add(entry);
        showItem(tests);
        this.backup.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                end();
            }
        });
    }

    public void showItem(List<Entry> entries) {
        ListAdapter<Entry> adapter = new ListAdapter<>(showList, "ListItem.fxml");
        adapter.setItems(entries);
    }
}
